package com.xsky.logic.tabplatfault;

import java.util.Date;

import junit.framework.TestCase;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

import com.xsky.common.util.XMLConverter;
import com.xsky.logic.platfault.PlatFaultLogic;
import com.xsky.operation.vo.TabPlatFault;

public class TabPlatLogicTest extends TestCase {

	TabPlatFault fault;

	PlatFaultLogic logic = new PlatFaultLogic();

	String xml;

	@Override
	protected void setUp() throws Exception {
		fault = new TabPlatFault();
		fault.setType("1");
		fault.setTimestamp(new Date());
		fault.setOperatorId("1");
		fault.setOccurTime(new Date());
		fault.setResolveTime(new Date());
		xml = XMLConverter.convertToXML(fault);
	}

	public void testAll() throws DocumentException {
		// test add
		String result = logic.add(xml);
		Document document = DocumentHelper.parseText(result);
		String uuid = document.selectSingleNode("//data//uuid").getText();

		assertNotNull(uuid);
		assertEquals(uuid.length(), 32);

		fault.setId(uuid);
		// test query
		String resultXML = logic.queryById(uuid);
		TabPlatFault queryFault = XMLConverter.convertToObject(resultXML, TabPlatFault.class);

		assertEquals(queryFault.getId(), uuid);
		assertEquals(queryFault.getOperatorId(), fault.getOperatorId());
		assertEquals(queryFault.getEngineerEmail(), fault.getEngineerEmail());
		assertEquals(queryFault.getCreatorId(), fault.getCreatorId());

		// test update
		queryFault.setOperatorId("2");

		logic.update(XMLConverter.convertToXML(queryFault));
		resultXML = logic.queryById(uuid);

		queryFault = XMLConverter.convertToObject(resultXML, TabPlatFault.class);
		assertEquals(queryFault.getOperatorId(), "2");

		// test delete
		result = logic.delete(uuid);
		assertEquals(result.contains("ok"), true);

		resultXML = logic.queryById(uuid);
		assertNull(resultXML);
	}

	public void testQueryByCondition() {
		addKnow(20);
		String xml = "";
		xml += "<data>";
		xml += " <type>1</type>";
		xml += " <status>0</status>";
		xml += " <operatorId>1</operatorId>";
		xml += " <createTime_from>2011-03-10</createTime_from>";
		xml += " <createTime_to>2011-04-10</createTime_to>";
		xml += " <occurTime_from>2011-03-10</occurTime_from>";
		xml += " <occurTime_to>2011-04-10</occurTime_to>";
		xml += " <pageNum>1</pageNum>";
		xml += " <numPerPage>30</numPerPage>";
		xml += "</data>";

		String returnXML = logic.queryByCondition(xml);
		System.out.println(returnXML);
	}

	private void addKnow(int num) {
		for (int i = 0; i < num; i++) {
			logic.add(xml);
		}
	}

}
